﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace M3u8Puller
{
    /// <summary>
    /// DESEncrypt加密解密算法。
    /// </summary>
    public class AESEncoder
    {
        public AESEncoder(string key)
        {
            this.Key = key;
        }

        /// <summary>
        /// 对称加密解密的密钥
        /// </summary>
        public string Key { get; private set; }

        /// <summary>
        /// DES加密
        /// </summary>
        /// <param name="encryptString"></param>
        /// <returns></returns>
        public string EncryptString(string data)
        {
            byte[] keyArray = UTF8Encoding.UTF8.GetBytes(this.Key);
            //byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(data);
            byte[] toEncryptArray = Convert.FromBase64String(data);
            RijndaelManaged rDel = new RijndaelManaged();
            rDel.Key = keyArray;
            rDel.Mode = CipherMode.CBC;
            rDel.Padding = PaddingMode.PKCS7;
            ICryptoTransform cTransform = rDel.CreateEncryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
            return Convert.ToBase64String(resultArray, 0, resultArray.Length);
        }

        /// <summary>
        /// DES解密
        /// </summary>
        /// <param name="decryptString"></param>
        /// <returns></returns>
        public string DecryptString(string data)
        {
            byte[] keyArray = UTF8Encoding.UTF8.GetBytes(this.Key);
            byte[] toEncryptArray = Convert.FromBase64String(data);
            RijndaelManaged rDel = new RijndaelManaged();
            rDel.Key = keyArray;
            rDel.Mode = CipherMode.CBC;
            rDel.Padding = PaddingMode.PKCS7;
            ICryptoTransform cTransform = rDel.CreateDecryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
            //return UTF8Encoding.UTF8.GetString(resultArray);
            return Convert.ToBase64String(resultArray);
        }

        /// <summary>
        /// 加密文件
        /// </summary>
        /// <param name="filepath">源文件</param>
        /// <param name="filepath2">输出文件</param>
        /// <returns></returns>
        public bool EncryptFile(string filepath, string filepath2)
        {
            try
            {
                var bytes = File.ReadAllBytes(filepath);
                //File.WriteAllBytes(filepath2, Convert.FromBase64String(this.EncryptString(Convert.ToBase64String(bytes))));
                File.WriteAllBytes(filepath2, EncryptBytes(bytes));
                return true;
            }
            catch (Exception ex)
            {
                Debug.Print(ex.ToString());
                return false;
            }
        }

        /// <summary>
        /// 解密文件
        /// </summary>
        /// <param name="filepath">源文件</param>
        /// <param name="filepath2">输出文件</param>
        /// <returns></returns>
        public bool DecryptFile(string filepath, string filepath2)
        {
            try
            {
                var bytes = File.ReadAllBytes(filepath);
                //File.WriteAllBytes(filepath2, Convert.FromBase64String(this.DecryptString(Convert.ToBase64String(bytes))));
                File.WriteAllBytes(filepath2, DecryptBytes(bytes));
                return true;
            }
            catch (Exception ex)
            {
                Debug.Print(ex.ToString());
                MessageBox.Show(ex.ToString());
                return false;
            }
        }

        /// <summary>
        /// DES加密
        /// </summary>
        /// <param name="encryptString"></param>
        /// <returns></returns>
        public byte[] EncryptBytes(byte[] data)
        {
            byte[] keyArray = UTF8Encoding.UTF8.GetBytes(this.Key);
            RijndaelManaged rDel = new RijndaelManaged();
            rDel.Key = keyArray;
            rDel.Mode = CipherMode.CBC;
            rDel.Padding = PaddingMode.PKCS7;
            ICryptoTransform cTransform = rDel.CreateEncryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(data, 0, data.Length);
            return resultArray;
        }

        /// <summary>
        /// DES解密
        /// </summary>
        /// <param name="decryptString"></param>
        /// <returns></returns>
        public byte[] DecryptBytes(byte[] data)
        {
            byte[] keyArray = UTF8Encoding.UTF8.GetBytes(this.Key);
            RijndaelManaged rDel = new RijndaelManaged();
            rDel.Key = keyArray;
            rDel.Mode = CipherMode.CBC;
            rDel.Padding = PaddingMode.PKCS7;
            ICryptoTransform cTransform = rDel.CreateDecryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(data, 0, data.Length);
            return resultArray;
        }
    }
}
